<?php
declare (strict_types=1);

namespace app\admin\service\advertiser;

use app\admin\model\advertiser\AdvertiserPage;
use app\admin\model\advertiser\AdvertiserPageUser;
use app\admin\model\advertiser\AdvertiserTask;
use app\admin\model\advertiser\AdvertiserUser;
use app\admin\validates\AdvertiserPageValidate;
use app\BaseService;
use think\exception\ValidateException;
use think\response\Json;

/**
 * 广告主页面广告位服务层
 */
class AdvertiserPageService extends BaseService
{
    ######################################## 页面广告位管理 ##############################################
    /**
     * 页面广告位列表
     * @param array $params
     * @return Json
     */
    public function pageList(array $params): Json
    {
        $where[] = ['closed', '=', 0];
        //搜索展示位编码
        if ($params['code']) {
            $where[] = ['code', 'like', '%' . $params['code'] . '%'];
        }
        //搜索展示位名称
        if ($params['name']) {
            $where[] = ['name', 'like', '%' . $params['name'] . '%'];
        }
        //搜索状态
        if ($params['status'] !== null and in_array($params['status'], [0, 1])) {
            $where[] = ['status', '=', $params['status']];
        }
        //获取列表
        $data = AdvertiserPage::getList($where, $params['page'], $params['limit'], 'id desc');
        return jsonReturn(1, $data);
    }

    /**
     * 页面广告位添加
     * @param array $params
     * @return Json
     */
    public function pageAdd(array $params): Json
    {
        //数据验证
        try {
            validate(AdvertiserPageValidate::class)->scene('add')->check($params);
        } catch (ValidateException $e) {
            return jsonReturn(0, [], $e->getError());
        }
        //验证位置编码是否唯一
        $res = AdvertiserPage::findOne(['code' => $params['code']]);
        if ($res) {
            return jsonReturn(0, [], '位置编码已存在');
        }
        //处理参数
        $params['device_type'] = json_encode(array_map('intval', $params['device_type']));
        $params['create_time'] = time();
        $params['update_time'] = time();
        //写入数据库
        $id = AdvertiserPage::insertGetId($params);
        if ($id) {
            return jsonReturn(1, ['id' => $id], '添加成功');
        }
        return jsonReturn(0, [], '添加失败');
    }

    /**
     * 页面广告位编辑
     * @param array $params
     * @return Json
     */
    public function pageEdit(array $params): Json
    {
        //数据验证
        try {
            validate(AdvertiserPageValidate::class)->scene('edit')->check($params);
        } catch (ValidateException $e) {
            return jsonReturn(0, [], $e->getError());
        }
        //获取当前模型
        $Model = AdvertiserPage::where(['id' => $params['id']])->find();
        if (!$Model) {
            return jsonReturn(0, [], '广告位不存在');
        }
        //验证位置编码是否唯一
        if ($Model->code != $params['code']) {
            $res = AdvertiserPage::findOne(['code' => $params['code']]);
            if ($res) {
                return jsonReturn(0, [], '位置编码已存在');
            }
        }
        //处理参数
        $params['device_type'] = json_encode(array_map('intval', $params['device_type']));
        $params['update_time'] = time();
        //写入数据库
        $id = $Model->save($params);
        if ($id) {
            return jsonReturn(1, [], '编辑成功');
        }
        return jsonReturn(0, [], '编辑失败');
    }

    /**
     * 页面广告位删除
     * @param array $params
     * @return Json
     */
    public function pageDel(array $params): Json
    {
        //数据验证
        try {
            validate(AdvertiserPageValidate::class)->scene('del')->check($params);
        } catch (ValidateException $e) {
            return jsonReturn(0, [], $e->getError());
        }
        //获取当前模型
        $Model = AdvertiserPage::findOne($params);
        if (!$Model) {
            return jsonReturn(0, [], '广告位不存在');
        }
        $res = AdvertiserPage::deleteData($params);
        if ($res) {
            return jsonReturn(1, [], '删除成功');
        }
        return jsonReturn(0, [], '删除失败');
    }

    /**
     * 页面广告位状态
     * @param array $params
     * @return Json
     */
    public function pageStatus(array $params): Json
    {
        //数据验证
        try {
            validate(AdvertiserPageValidate::class)->scene('status')->check($params);
        } catch (ValidateException $e) {
            return jsonReturn(0, [], $e->getError());
        }
        //获取当前模型
        $Model = AdvertiserPage::where(['id' => $params['id']])->find();
        if (!$Model) {
            return jsonReturn(0, [], '广告位不存在');
        }
        //写入数据库
        $res = $Model->save($params);
        if ($res) {
            return jsonReturn(1, [], '编辑成功');
        }
        return jsonReturn(0, [], '编辑失败');
    }

    ###################################### 页面广告位授权广告主 ################################################

    /**
     * 页面广告位授权广告主列表
     * @param array $params
     * @return Json
     */
    public function pageAuthList(array $params): Json
    {
        //获取当前模型
        $Model = AdvertiserPage::findOne(['id' => $params['page_id']]);
        if (!$Model) {
            return jsonReturn(0, [], '广告位不存在');
        }
        //获取列表
        $where = ['page_id' => $params['page_id']];
        $data = AdvertiserPageUser::getList($where, $params['page'], $params['limit'], 'update_time desc');
        return jsonReturn(1, $data);
    }

    /**
     * 页面广告位新增授权广告主
     * @param array $params
     * @return Json
     */
    public function pageAuthAdd(array $params): Json
    {
        //判断参数
        if (!$params['page_id'] or !$params['ad_user_id'] or !$params['device_type']) {
            return jsonReturn(0, [], '参数错误');
        }
        //判断广告位是否存在
        $Model = AdvertiserPage::findOne(['id' => $params['page_id']]);
        if (!$Model) {
            return jsonReturn(0, [], '广告位不存在');
        }
        //判断广告主是否存在
        $Model = AdvertiserUser::findOne(['id' => $params['ad_user_id']]);
        if (!$Model) {
            return jsonReturn(0, [], '广告主不存在');
        }
        //处理参数
        $params['device_type'] = json_encode(array_map('intval', $params['device_type']));
        $params['update_time'] = time();
        //获取当前模型
        $where = ['page_id' => $params['page_id'], 'ad_user_id' => $params['ad_user_id']];
        $Model = AdvertiserPageUser::where($where)->find();
        if (!$Model) {
            $Model = new AdvertiserPageUser();
        }
        //写入数据库
        $res = $Model->save($params);
        if ($res) {
            return jsonReturn(1, [], '添加成功');
        }
        return jsonReturn(0, [], '添加失败');
    }

    /**
     * 页面广告位修改授权广告主
     * @param array $params
     * @return Json
     */
    public function pageAuthEdit(array $params): Json
    {
        //判断参数
        if (!$params['page_id'] or !$params['ad_user_id'] or !$params['device_type']) {
            return jsonReturn(0, [], '参数错误');
        }
        //判断广告位是否存在
        $Model = AdvertiserPage::findOne(['id' => $params['page_id']]);
        if (!$Model) {
            return jsonReturn(0, [], '广告位不存在');
        }
        //判断广告主是否存在
        $Model = AdvertiserUser::findOne(['id' => $params['ad_user_id']]);
        if (!$Model) {
            return jsonReturn(0, [], '广告主不存在');
        }
        //处理参数
        $params['device_type'] = json_encode(array_map('intval', $params['device_type']));
        $params['update_time'] = time();
        //获取当前模型
        $where = ['page_id' => $params['page_id'], 'ad_user_id' => $params['ad_user_id']];
        $Model = AdvertiserPageUser::where($where)->find();
        if (!$Model) {
            return jsonReturn(0, [], '当前授权不存在');
        }
        //写入数据库
        $res = $Model->save($params);
        if ($res) {
            return jsonReturn(1, [], '编辑成功');
        }
        return jsonReturn(0, [], '编辑失败');
    }

    /**
     * 页面广告位删除授权广告主
     * @param array $params
     * @return Json
     */
    public function pageAuthDel(array $params): Json
    {
        //判断参数
        if (!$params['page_id'] or !$params['ad_user_id']) {
            return jsonReturn(0, [], '参数错误');
        }
        //获取当前模型
        $Model = AdvertiserPageUser::findOne($params);
        if (!$Model) {
            return jsonReturn(0, [], '广告位授权不存在');
        }
        $res = AdvertiserPageUser::deleteData($params, true);
        if ($res) {
            return jsonReturn(1, [], '删除成功');
        }
        return jsonReturn(0, [], '删除失败');
    }

    ###################################### 页面广告位任务列表 ################################################

    /**
     * 广告主推广任务列表
     * @param array $params
     * @return Json
     */
    public function pageTaskList(array $params): Json
    {
        $where[] = ['type', '=', 3];
        $where[] = ['closed', '=', 0];
        //搜索任务名称
        if ($params['task_name']) {
            $where[] = ['task_name', 'like', '%' . $params['task_name'] . '%'];
        }
        //搜索广告主id
        if ($params['ad_user_id']) {
            $where[] = ['ad_user_id', '=', $params['ad_user_id']];
        }
        //搜索状态
        if ($params['status'] and in_array($params['status'], [1, 2, 3, 4, 5, 6])) {
            $where[] = ['status', '=', $params['status']];
        }
        //创建时间筛选
        if ($params['start_time'] && $params['end_time']) {
            $start_time = (int)$params['start_time'];
            $end_time = (int)$params['end_time'];
            $where[] = ['create_time', 'between', [$start_time, $end_time]];
        }
        //获取列表
        $data = AdvertiserTask::getPageList($where, $params['page'], $params['limit'], 'id desc');
        return jsonReturn(1, $data);
    }

}
